package com.wenc.psi.dao;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.wenc.psi.utils.JDBCUtils;
import com.wenc.psi.pojo.Part;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PartDao extends JDBCUtils implements Dao<Part> {
    @Override
    public void insert(Part... parts) {

    }

    @Override
    public void delete(Part... parts) {

    }

    @Override
    public void delete(String... ids) {

    }

    @Override
    public void update(Part oldT, Part newT) {

    }

    public List<Part> getDataBySearch(String input){
        List<Part> partList = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "SELECT * FROM part WHERE id LIKE '%" + input + "%' OR name LIKE '%" + input + "%' OR pinyin LIKE '%" + input +"%'";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Part part = new Part();
                part.setId(rs.getString("id"));
                part.setName(rs.getString("name"));
                part.setPinyin(rs.getString("pinyin"));
                part.setModel(rs.getString("model"));
                part.setEngine(rs.getString("engine"));
                part.setInventory(rs.getInt("inventory"));
                part.setPrice(rs.getDouble("price"));
                partList.add(part);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,stmt,conn);
        }
        return partList;
    }

    @Override
    public List<Part> getAllData() {
            List<Part> partList = new ArrayList<>();
            try {
                conn = getConnection();
                String sql = "SELECT * FROM part";
                pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery();
                while (rs.next()){
                    Part part = new Part();
                    part.setId(rs.getString("id"));
                    part.setName(rs.getString("name"));
                    part.setPinyin(rs.getString("pinyin"));
                    part.setModel(rs.getString("model"));
                    part.setEngine(rs.getString("engine"));
                    part.setInventory(rs.getInt("inventory"));
                    part.setPrice(rs.getDouble("price"));
                    partList.add(part);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                close(rs,stmt,conn);
            }
            return partList;
    }

    @Override
    public Part getDataById(String id) {
        return null;
    }

    @Override
    public LiveData<List<Part>> getAllLiveData() {
        MutableLiveData<List<Part>> partLive = new MutableLiveData<>();
        List<Part> partList = new ArrayList<>();
        try {
            conn = getConnection();
            String sql = "SELECT * FROM part";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()){
                Part part = new Part();
                part.setId(rs.getString("id"));
                part.setName(rs.getString("name"));
                part.setPinyin(rs.getString("pinyin"));
                part.setModel(rs.getString("model"));
                part.setEngine(rs.getString("engine"));
                part.setInventory(rs.getInt("inventory"));
                part.setPrice(rs.getDouble("price"));
                partList.add(part);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,stmt,conn);
        }
        partLive.setValue(partList);
        return partLive;
    }
}
